package com.qf.mrmd.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.awt.print.PrinterAbortException;

@Configuration
public class CrosConfig implements WebMvcConfigurer {

    @Autowired
    private AuthorizedInterceptor authorizedInterceptor;

    public static final String[] METHODS = {"GET","POST","PUT","PATCH","DELETE"};

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //拦截所有请求
        registry.addMapping("/**")
                .allowedOriginPatterns("http://127.0.0.1:8848") //拦截地址，可设为具体，*表示全拦截
                .allowedHeaders("*")    //设置请求头，*所有信息都可携带
                .allowedMethods(METHODS)
                .allowCredentials(true)   //是否允许携带cookie
                .maxAge(6000);

    }


    //拦截器

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //需要一个拦截器对象
        registry.addInterceptor(authorizedInterceptor);
    }
}
